﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace MapEditor
{
  

    public class ObjectTemplateManager
    {
        public static List<SLGameEngine.ObjectBase> ObjectBaseTemplates = new List<SLGameEngine.ObjectBase>();
        public static Dictionary<int, SLGameEngine.ObjectBase> ObjectTemplateDictionary = new Dictionary<int, SLGameEngine.ObjectBase>();

        private static ObjectLoadComplete OnLoadComplete;

        public static void LoadTemplateObjects(ObjectLoadComplete onLoadComplete)
        {
            OnLoadComplete = onLoadComplete;

            ServiceReference1.MapEditorServiceClient client = new MapEditor.ServiceReference1.MapEditorServiceClient();
            client.GetBaseObjectsCompleted += new EventHandler<MapEditor.ServiceReference1.GetBaseObjectsCompletedEventArgs>(client_GetBaseObjectsCompleted);
            client.GetBaseObjectsAsync();
        }

        public static SLGameEngine.ObjectBase GetBaseObject(int ID)
        {
            if (ObjectTemplateDictionary.ContainsKey(ID))
                return ObjectTemplateDictionary[ID];

            return null;
        }

        private static void client_GetBaseObjectsCompleted(object sender, MapEditor.ServiceReference1.GetBaseObjectsCompletedEventArgs e)
        {
            foreach (ServiceReference1.BaseObject baseObj in e.Result)
            {
                switch (baseObj.ObjType)
                {
                    case ServiceReference1.ObjectType.Creature:
                        CreateCreatureTemplate(baseObj);
                        break;
                    case ServiceReference1.ObjectType.Effect:
                       // CreateEffectTemplate(baseObj);
                        break;
                    case ServiceReference1.ObjectType.Game:
                        CreateGameTemplate(baseObj);
                        break;
                    case ServiceReference1.ObjectType.Map:
                        CreateMapTemplate(baseObj);
                        break;
                    case ServiceReference1.ObjectType.Terrain:
                        CreateTerrainTemplate(baseObj);
                        break;
                }
            }
            OnLoadComplete();
        }

        private static void CreateGameTemplate(ServiceReference1.BaseObject baseObj)
        {
            SLGameEngine.ObjectGame gameObj = new SLGameEngine.ObjectGame(0, 0, 1, baseObj.BaseObjectID);
            gameObj.ObjectName = baseObj.Name;
            gameObj.IsWalkable = baseObj.Walkable;
            gameObj.IsTargetable = baseObj.Targetable;
            gameObj.IsAttackable = baseObj.Attackable;
            gameObj.SetWidth(baseObj.Width);
            gameObj.SetHeight(baseObj.Height);
            MapEditor.ServiceReference1.ObjectImage objectImg = baseObj.ObjImages[0];
            gameObj.SetImage(0, objectImg.ImageUrl);
            gameObj.SetPreviewImage(objectImg.PreviewUrl);

            ObjectTemplateDictionary.Add(baseObj.BaseObjectID, gameObj);

            ObjectBaseTemplates.Add(gameObj);
        }

        private static void CreateMapTemplate(ServiceReference1.BaseObject baseObj)
        {
            SLGameEngine.ObjectMap mapObj = new SLGameEngine.ObjectMap(0, 0, 1, baseObj.BaseObjectID);
            mapObj.ObjectName = baseObj.Name;
            mapObj.IsWalkable = baseObj.Walkable;
            mapObj.IsTargetable = baseObj.Targetable;
            mapObj.IsAttackable = baseObj.Attackable;
            mapObj.SetWidth(baseObj.Width);
            mapObj.SetHeight(baseObj.Height);
            MapEditor.ServiceReference1.ObjectImage objectImg = baseObj.ObjImages[0];
            mapObj.SetImage(0, objectImg.ImageUrl);
            mapObj.SetPreviewImage(objectImg.PreviewUrl);

            ObjectTemplateDictionary.Add(baseObj.BaseObjectID, mapObj);

            ObjectBaseTemplates.Add(mapObj);
        }

        private static void CreateTerrainTemplate(ServiceReference1.BaseObject baseObj)
        {
            SLGameEngine.ObjectTerrain terrainObj = new SLGameEngine.ObjectTerrain(0, 0, baseObj.Width, SLGameEngine.Consts.TerrainLayerCount, baseObj.BaseObjectID);
            terrainObj.ObjectName = baseObj.Name;
            terrainObj.IsWalkable = baseObj.Walkable;
            terrainObj.IsTargetable = baseObj.Targetable;
            terrainObj.IsAttackable = baseObj.Attackable;
            terrainObj.SetWidth(baseObj.Width);
            terrainObj.SetHeight(baseObj.Height);
            MapEditor.ServiceReference1.ObjectImage objectImg = baseObj.ObjImages[0];
            terrainObj.SetLayer(0, baseObj.BaseObjectID, objectImg.ImageUrl);

            ObjectTemplateDictionary.Add(baseObj.BaseObjectID, terrainObj);

            ObjectBaseTemplates.Add(terrainObj);
        }

        private static void CreateCreatureTemplate(ServiceReference1.BaseObject baseObj)
        {
            SLGameEngine.ObjectCreature creatureObj = new SLGameEngine.ObjectCreature(0, 0, baseObj.BaseObjectID);
            creatureObj.ObjectName = baseObj.Name;
            creatureObj.IsWalkable = baseObj.Walkable;
            creatureObj.IsTargetable = baseObj.Targetable;
            creatureObj.IsAttackable = baseObj.Attackable;
            creatureObj.SetWidth(baseObj.Width);
            creatureObj.SetHeight(baseObj.Height);

            ObjectTemplateDictionary.Add(baseObj.BaseObjectID, creatureObj);

            foreach (MapEditor.ServiceReference1.ObjectImage objectImg in baseObj.ObjImages)
            {
                creatureObj.AddImageFrame((SLGameEngine.Enums.Direction)objectImg.Direction, (SLGameEngine.Enums.AnimationType)objectImg.AnimationType, objectImg.FrameCount, objectImg.FramesPerRow, objectImg.ImageUrl);
            }

            ObjectBaseTemplates.Add(creatureObj);
        }
    }
    
    public class OpacityResource
    {
        public int OpacityMaskID;
        public string OpacityUrl;
        public string Name;
        public Image OpacityImage;
    }

    public class OpacityMasksManager
    {
        public static List<OpacityResource> OpacityMasks = new List<OpacityResource>();
        private static ObjectLoadComplete OnLoadComplete;

        public static void LoadMasksResources(ObjectLoadComplete onLoadComplete)
        {
            OnLoadComplete = onLoadComplete;
            ServiceReference1.MapEditorServiceClient client = new MapEditor.ServiceReference1.MapEditorServiceClient();
            client.GetOpacityObjectsCompleted += new EventHandler<MapEditor.ServiceReference1.GetOpacityObjectsCompletedEventArgs>(client_GetOpacityObjectsCompleted);
            client.GetOpacityObjectsAsync();
        }

        static void client_GetOpacityObjectsCompleted(object sender, MapEditor.ServiceReference1.GetOpacityObjectsCompletedEventArgs e)
        {
            foreach (ServiceReference1.OpacityResource opRes in e.Result)
            {
                OpacityResource or = new OpacityResource();
                or.OpacityUrl = opRes.OpacityUrl;
                or.OpacityImage = new Image();
                or.OpacityImage.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(OpacityImage_ImageFailed);
                or.OpacityImage.Source = SLGameEngine.Utils.LoadImage(opRes.OpacityUrl);
                or.Name = opRes.Name;
                opRes.OpacityMaskID = opRes.OpacityMaskID;
                OpacityMasks.Add(or);
            }
            OnLoadComplete();
        }

        static void OpacityImage_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            
        }
    }
}
